﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Text;
using Model;

namespace BusinessLogic
{
    public class TXSearchCommand : BaseCommand<List<acm_tx_search>>
    {
        private bool isCount { get; set; }
        public int ReccordCount { get; set; }

        public TXSearchCommand(string vin, string srt, string end, string dealerCode, string stat)
        {
            //Count
            SetQuery(String.Format("exec ACM_TX_SEARCH '{0}',{1},{2},'{3}','{4}',null,null", vin, srt, end, dealerCode, stat));
            this.isCount = true;
        }

        public TXSearchCommand(string vin, string srt, string end, string dealerCode, string stat, string pageId, string rowPerPage)
        {
            //Search
            SetQuery(String.Format("exec ACM_TX_SEARCH '{0}',{1},{2},'{3}','{4}',{5},{6}", vin, srt, end, dealerCode, stat, pageId, rowPerPage));
            this.isCount = false;
        }

        public override void Command()
        {
            Result = new List<acm_tx_search>();
            SqlDataReader dr = cmd.ExecuteReader();

            if (isCount)
            {
                dr.Read();
                if (dr.HasRows)
                {
                    ReccordCount = dr.GetInt32(0);
                }
            }
            else
            {
                while (dr.Read())
                {
                    acm_tx_search row = new acm_tx_search();
                    row.id = GetNullableInt(dr, 0);
                    row.dealer_name = GetNullableString(dr, 1);
                    row.tx_dealer_code = GetNullableString(dr, 2);
                    row.owner_dealer = GetNullableString(dr, 3);
                    row.customer_name = GetNullableString(dr, 4);
                    row.vin = GetNullableString(dr, 5);
                    row.car_series = GetNullableString(dr, 6);
                    row.car_sub_series = GetNullableString(dr, 7);
                    row.dlv_date = GetNullableDateTime(dr, 8);
                    row.tx_date = GetNullableDateTime(dr, 9);
                    row.campaign_name = GetNullableString(dr, 10);
                    var aa = GetNullableDouble(dr, 11);
                    row.diff_promotion_amount = aa != null ? Convert.ToDecimal(aa) : 0;
                    row.mobile_note4 = GetNullableInt(dr, 12);
                    row.tx_status = GetNullableString(dr, 13);
                    row.RowNumber = GetNullableInt(dr, 14);
                    row.adv_date = GetNullableDateTime(dr, 15);
                    row.car_suffix = GetNullableString(dr, 16);
                    row.approve_date = GetNullableDateTime(dr, 17);

                    Result.Add(row);
                }
            }
        }
    }
}
